/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vng.zing.zprofilemiddleware.handlers;

import com.vng.zing.calc.thrift.Profile;
import com.vng.zing.calc.thrift.ProfilePage;
import com.vng.zing.calc.thrift.ROLE;
import com.vng.zing.calc.thrift.SOURCE;
import com.vng.zing.calc.thrift.STATUS;
import com.vng.zing.calc.thrift.ZProfileService;
import com.vng.zing.logger.ZLogger;
import com.vng.zing.stats.Profiler;
import com.vng.zing.stats.ThreadProfiler;
import com.vng.zing.zprofilemiddleware.model.ZProfileModel;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.thrift.TException;

/**
 * @Note: Class handler xử lý thrift request, class handler được thiết kế để cho
 * phép tạo rất nhiều object, mỗi object tương đương với 1 request đầu vào
 * (object tạm thời), phần xử lý (implement IFace) chỉ đơn giản gọi lại phần xử
 * lý trong Model tương ứng
 *
 * => Không nên tạo thuộc tính thành viên trong class handler (vì sẽ có rất
 * nhiều object handler được tạo ra), hoặc chỉ tạo các thuộc tính primitive
 * types (boolean, char, short, int, long, float, double), hoặc chỉ tạo thuộc
 * tính static. Nếu cần thì hãy thêm trong class model tương ứng.
 *
 * @author namnq
 */
public class ZProfileServiceHandle implements ZProfileService.Iface {

    private static final Logger _Logger = ZLogger.getLogger(ZProfileServiceHandle.class);

    @Override
    public Profile createProfile(Profile profile) throws TException {
        ThreadProfiler profiler = Profiler.createThreadProfiler("TCalcHandler.getProfile", false);
        try {
            return ZProfileModel.Instance.createProfile(profile);
        } finally {
            Profiler.closeThreadProfiler();
        }
    }

    @Override
    public Profile logIn(String username, String password) throws TException {
        ThreadProfiler profiler = Profiler.createThreadProfiler("TCalcHandler.getProfile", false);
        try {
            return ZProfileModel.Instance.logIn(username, password);
        } finally {
            Profiler.closeThreadProfiler();
        }
    }

    @Override
    public Profile getProfile(int profileId) throws TException {
        ThreadProfiler profiler = Profiler.createThreadProfiler("TCalcHandler.getProfile", false);
        try {
            return ZProfileModel.Instance.getProfile(profileId);
        } finally {
            Profiler.closeThreadProfiler();
        }
    }

    @Override
    public Profile updateProfile(Profile profile) {
        ThreadProfiler profiler = Profiler.createThreadProfiler("TCalcHandler.getProfile", false);
        try {
            return ZProfileModel.Instance.updateProfile(profile);
        } finally {
            Profiler.closeThreadProfiler();
        }
    }

    @Override
    public Profile changePassword(int profileId, String currentPassword, String newPassword) throws TException {
        ThreadProfiler profiler = Profiler.createThreadProfiler("TCalcHandler.getProfile", false);
        try {
            return ZProfileModel.Instance.changePassword(profileId, currentPassword, newPassword);
        } finally {
            Profiler.closeThreadProfiler();
        }
    }

    @Override
    public Map<Short, String> getAllCity() throws TException {
        ThreadProfiler profiler = Profiler.createThreadProfiler("TCalcHandler.getProfile", false);
        try {
            return ZProfileModel.Instance.getAllCity();
        } finally {
            Profiler.closeThreadProfiler();
        }
    }

    @Override
    public boolean validUsername(String username) throws TException {
        ThreadProfiler profiler = Profiler.createThreadProfiler("TCalcHandler.getProfile", false);
        try {
            return ZProfileModel.Instance.validUsername(username);
        } finally {
            Profiler.closeThreadProfiler();
        }
    }

    @Override
    public ProfilePage getAllProfile(int limit, int offset) {
        ThreadProfiler profiler = Profiler.createThreadProfiler("TCalcHandler.getProfile", false);
        try {
            return ZProfileModel.Instance.getAllProfile(limit, offset);
        } finally {
            Profiler.closeThreadProfiler();
        }
    }

    @Override
    public boolean changeStatus(int profileId, STATUS status)   {
        ThreadProfiler profiler = Profiler.createThreadProfiler("TCalcHandler.getProfile", false);
        try {
            return ZProfileModel.Instance.changeStatus(profileId, status);
        } finally {
            Profiler.closeThreadProfiler();
        }
    }

    @Override
    public Profile getSocialProfile(SOURCE source, String UID)  {
        ThreadProfiler profiler = Profiler.createThreadProfiler("TCalcHandler.getProfile", false);
        try {
            return ZProfileModel.Instance.getSocialProfile(source, UID);
        } finally {
            Profiler.closeThreadProfiler();
        }
    }

}
